﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Retirement_changeDetails : System.Web.UI.Page
{
    PolicyRules policyRules = new PolicyRules();
    Customer customer = new Customer();
    Policy policy = new Policy();
    public List<Policy> policies = new List<Policy>();

    protected void Page_Load(object sender, EventArgs e)
    {
        int custId = -1;

        if (Session["userID"] == null)
        {
            custId = 15369472;//TODO GET FROM SESSION

        }
        else
        {
            custId = Convert.ToInt32(Session["userID"]);

        }


        Customer customer = Customer.getCustomerInfo(custId);
        this.policies = Policy.getPolicy(custId);
        if (policies == null || policies.Count == 0)
        {
            return;
        }
        else
        {
            Policy policy = policies.ElementAt(0);

            this.lb_firstName.Text = customer.firstName;
            this.lb_lastName.Text = customer.lastName;
            this.lb_nric.Text = customer.nric;
            this.lb_nationality.Text = customer.nationality;
            this.lb_dob.Text = String.Format("{0:dd/MM/yyyy}", customer.dob);
            this.lb_maritalStatus.Text = customer.maritalStatus;

            if ("Male".Equals(customer.gender))
            {
                rb_male.Checked = true;
                rb_female.Checked = false;
            }
            else
            {
                rb_male.Checked = false;
                rb_female.Checked = true;
            }

            if ("Y".Equals(customer.smoker))
            {
                rb_smoker.Checked = true;
                rb_nonSmoker.Checked = false;
            }
            else
            {
                rb_smoker.Checked = false;
                rb_nonSmoker.Checked = true;
            }

            this.lb_mailingAddress.Text = customer.mailingAddress;
            this.lb_postalCode.Text = customer.postalCode;
            this.lb_mobileNo.Text = customer.mobileNo;
            this.lb_telNo.Text = customer.telNo;
            this.lb_email.Text = customer.email;

            //Occupational Details
            this.lb_occupation.Text = customer.occupation;
            this.lb_jobDuties.Text = customer.jobDuties;
            this.lb_employer.Text = customer.employer;
            this.lb_noOfYears.Text = Convert.ToString(customer.noOfYears);
            this.lb_industry.Text = customer.industry;
            this.lb_annualEarnedIncome.Text = Convert.ToDecimal(customer.annualIncomeCurrent).ToString("###,###.00");
            this.lb_annualEarnedIncomePrevious.Text = Convert.ToDecimal(customer.annualIncomePrevious).ToString("###,###.00");

            if (!"".Equals(customer.otherIncomeSource))
            {
                rb_yesOtherIncome.Checked = true;
                rb_noOtherIncome.Checked = false;
            }
            else
            {
                rb_yesOtherIncome.Checked = false;
                rb_noOtherIncome.Checked = true;
            }

            this.lb_otherIncomeSource.Text = customer.otherIncomeSource;
            this.lb_otherAnnualEarnedIncome.Text = Convert.ToDecimal(customer.otherSourceIncome).ToString("###,###.00");
            this.lb_sourceOfWealth.Text = customer.sourceOfWealth;

            if ("Y".Equals(customer.bankruptcy))
            {
                rb_yesBankruptcy.Checked = true;
                rb_noBankruptcy.Checked = false;
            }
            else
            {
                rb_yesBankruptcy.Checked = false;
                rb_noBankruptcy.Checked = true;
            }

            //Plan Details
            this.lb_planName.Text = "Flexi Protect";
            this.lb_basicSumInsured.Text = policy.basicSumInsured.ToString("###,###.00");

            int age = -1;
            age = Util.getAge(customer.dob);
            if (age >= 0)
            {
                this.lb_basicPlanDuration.Text = Convert.ToString(99 - age) + " Years";
            }

            if (customer.plusRiderSumInsured != 0)
            {
                rb_plusRiderYes.Checked = true;
                rb_plusRiderNo.Checked = false;
                this.lb_plusRiderDuration.Text = Convert.ToString(65 - age) + " Years";
                this.lb_plusRiderSumInsured.Text = policy.plusRiderSumInsured.ToString("###,###.00");
            }
            else
            {
                rb_plusRiderYes.Checked = false;
                rb_plusRiderNo.Checked = true;
            }

            if (customer.waiverRiderPremium != 0)
            {
                rb_waiverRiderYes.Checked = true;
                rb_waiverRiderNo.Checked = false;
                this.lb_waiverRiderDuration.Text = Convert.ToString(65 - age) + " Years";
            }
            else
            {
                rb_waiverRiderYes.Checked = false;
                rb_waiverRiderNo.Checked = true;
            }

            this.lb_totalAnnualPremium.Text = policy.totalAnnualPremium.ToString("###,###.00");


            //Fund Allocation
            List<Fund> choosenFundAllocation = Fund.getActivePortfolioFunds(policy.policyNo);

            this.gv_choosenFundAllocation.DataSource = choosenFundAllocation;
            this.gv_choosenFundAllocation.DataBind();

            //Payment Details
            if ("M".Equals(policy.premiumFrequency))
            {
                rb_monthly.Checked = true;
                rb_semiAnnually.Checked = false;
                rb_annually.Checked = false;
            }
            else if ("S".Equals(policy.premiumFrequency))
            {
                rb_monthly.Checked = false;
                rb_semiAnnually.Checked = true;
                rb_annually.Checked = false;
            }
            else
            {
                rb_monthly.Checked = false;
                rb_semiAnnually.Checked = false;
                rb_annually.Checked = true;
            }

            if ("Auto".Equals(policy.paymentMethod))
            {
                rb_auto.Checked = true;
                rb_direct.Checked = false;
            }
            else
            {
                rb_auto.Checked = false;
                rb_direct.Checked = true;
            }

            UserAccount userAccount = UserAccount.getUserAccountByAccountId(policy.accountId);
            this.lb_accountNumber.Text = userAccount.branchCode + "-" + userAccount.accountNumber + "-" + userAccount.accountTypeCode;
            this.lb_accountType.Text = userAccount.accountType;
            this.lb_currency.Text = userAccount.currency;


        }
        
    }
    protected void btn_changeDetails_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Redirect("./changeDetails.aspx");
    }
}